<!DOCTYPE html>
<html lang="zh-cn">
<head>
  
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>
OpenLDAP添加Schema（Ubuntu） |
穷折腾</title>
  <link rel="stylesheet" href="/static/css/style.css" />
  <link rel="stylesheet" href="/static/css/pygments.css" />
  <link rel="alternate" type="application/rss+xml" title="RSS" href="http://blog.zorro.im/rss.xml" />
  <link rel="icon" type="image/png" href="/static/img/favicon.png">
</head>
<body>
    <div class="page-wrap">
        <section class="main-header regular">
            <div class="vertical-container">
                <div class="wrapper clearfix">
                    <header>
                        <a href="/">
                            <h1 id="main-title">穷折腾</h1>
                            <h2 id="main-subtitle">zqqf16 的个人博客</h2>
                        </a>
                    </header>
                    <nav id="main-nav" role="navigation">
                        <a href="/" class="selected">主页</a>
                        <a href="/posts/about.html" class="">关于</a>
                        <a href="https://github.com/zqqf16">Github</a>
                    </nav>
                </div>
            </div>
        </section>
        <section class="main-content">
            
<article class="main-content">
    <div class="wrapper">
        <header class="section-header">
            <h1 class="section-title">OpenLDAP添加Schema（Ubuntu）</h1>
            <span class="section-subtitle"><time datetime="2013-08-19" pubdate="">2013-08-19</time></span>
        </header>
        <section class="post-content">
        	<h2 id="_1">前言</h2>
<p>工作中遇到了需要给LDAP添加自定义字段的情况，介于公司中此技艺已经失传，只能自己来了。</p>
<p>网络上很多流传的教程都太老了，不是很适用。Ubuntu8.10以及之后的系统倾向于用slapd-config的各种工具来配置sladp，而不是之前的直接修改文件的方法。</p>
<p>用sldap-config来添加Schema可以总结为以下几步：</p>
<ol>
<li>创建Schema文件</li>
<li>将Schema转换成ldif格式文件</li>
<li>将ldif文件内容导入</li>
</ol>
<h2 id="_2">具体步骤</h2>
<ol>
<li>
<p>编辑Schema文件，保存为<code>test.schema</code></p>
<div class="codehilite"><pre>objectIdentifier testOID 1.1.1.1
objectIdentifier testAttr testOID:1
objectIdentifier testObject testOID:2

attributetype ( testAttr
    NAME &#39;testattr&#39;
    DESC &#39;Test attribute&#39;
    EQUALITY caseIgnoreMatch
    SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )

objectclass ( testObject
    NAME &#39;testObject&#39;
    DESC &#39;Just for test&#39;
    AUXILIARY
    MAY (testattr))
</pre></div>


</li>
<li>
<p>创建文件<code>tmp.conf</code>,加入以下内容</p>
<div class="codehilite"><pre>include test.schema
</pre></div>


</li>
<li>
<p>创建目录<code>ldif_dir</code></p>
<div class="codehilite"><pre>$mkdir ldif_dir
</pre></div>


</li>
<li>
<p>生成‘ldif’文件</p>
<div class="codehilite"><pre>$slaptest -f tmp.conf -F ldif_dir
</pre></div>


<p>ldif目录结构如下：</p>
<div class="codehilite"><pre>.
|-- cn=config
|   |-- cn=schema
|   |   `-- cn={0}test.ldif
|   |-- cn=schema.ldif
|   |-- olcDatabase={0}config.ldif
|   `-- olcDatabase={-1}frontend.ldif
`-- cn=config.ldif
</pre></div>


</li>
<li>
<p>文件<code>cn=config/cn=schema/cn={0}test.ldif</code>就是生成的‘ldif’文件，编辑此文件：</p>
<p>将</p>
<div class="codehilite"><pre>dn: cn={0}test
objectClass: olcSchemaConfig
cn: {0}test
</pre></div>


<p>修改为</p>
<div class="codehilite"><pre>dn: cn=test,cn=schema,cn=config
objectClass: olcSchemaConfig
cn: test
</pre></div>


<p>删除以下几行：</p>
<div class="codehilite"><pre>structuralObjectClass: olcSchemaConfig
entryUUID: 9530cb4a-9845-1032-9b5c-15d9e32663bc
creatorsName: cn=config
createTimestamp: 20130813092213Z
entryCSN: 20130813092213.368308Z#000000#000#000000
modifiersName: cn=config
modifyTimestamp: 20130813092213Z
</pre></div>


<p>最终文件变为</p>
<div class="codehilite"><pre>dn: cn=test,cn=schema,cn=config
objectClass: olcSchemaConfig
cn: test
olcObjectIdentifier: {0}testOID 1.1.1.1
olcObjectIdentifier: {1}testAttr testOID:1
olcObjectIdentifier: {2}testObject testOID:2
olcAttributeTypes: {0}( testAttr NAME &#39;testattr&#39; DESC &#39;Test attribute&#39; E    QUALIT Y caseIgnoreMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
olcObjectClasses: {0}( testObject NAME &#39;testObject&#39; DESC &#39;Just for test&#39;     AUXILIARY MAY testattr )
</pre></div>


</li>
<li>
<p>将‘ldif’文件内容导入ldap数据库</p>
<div class="codehilite"><pre>$sudo ldapadd -Q -Y EXTERNAL -H ldapi:/// -f cn\=test.ldif
</pre></div>


</li>
<li>
<p>检查导入结果</p>
<div class="codehilite"><pre>$sudo ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -b cn=schema,cn=config dn
</pre></div>


</li>
</ol>
        </section>
        <div class="post-nav">
    		<!-- Duoshuo Comment BEGIN -->
<div class="ds-thread"></div>
<script type="text/javascript">
	var duoshuoQuery = {short_name:"zqqf16"};
	(function() {
		var ds = document.createElement('script');
		ds.type = 'text/javascript';ds.async = true;
		ds.src = 'http://static.duoshuo.com/embed.js';
		ds.charset = 'UTF-8';
		(document.getElementsByTagName('head')[0]
		|| document.getElementsByTagName('body')[0]).appendChild(ds);
	})();
</script>
<!-- Duoshuo Comment END -->

    	</div>
    </div>

</article>

        </section>
    </div>
    <footer class="main-footer">
        <div class="vertical-container">
            <div class="wrapper">
                <p class="copy">&copy; zqqf16</p>
            </div>
        </div>
    </footer>
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-41282906-2', 'auto');
      ga('require', 'displayfeatures');
      ga('send', 'pageview');
    </script>
</body>
</html>
